package com.athuahua.spzx.utils;

import com.athuahua.spzx.deprecated_entity.system.SysMenuHasChildren;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper{
        //通过递归实现封装过程
        public static List<SysMenuHasChildren> buildTree(List<SysMenuHasChildren> nodes) {

            List<SysMenuHasChildren> trees = new ArrayList<>();
            for (SysMenuHasChildren node : nodes) {
                //parent_id 为0时则为顶层数据
                if (node.getParentId() == 0) {
                    //根据第一层找下层数据
                    trees.add(findChildren(node, nodes));
                }
            }

            return trees;
        }

        private static SysMenuHasChildren findChildren(SysMenuHasChildren node, List<SysMenuHasChildren> nodes) {
            //找到当前节点的子节点
            List<SysMenuHasChildren> children = new ArrayList<>();
            for (SysMenuHasChildren n : nodes) {
                if (n.getParentId() == node.getId()) {
                    children.add(findChildren(n, nodes));
                }
            }
            node.setChildren(children);
            return node;
        }
    }